<?php

namespace Biz\model\wiki;


class WikiAbnormalLog extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki_abnormal_log';

    /**
     * 更新异常次数（异常次数+1，非异常次数置0）
     * @param $wikiId
     * @param bool $isAbnormal 是否异常
     * @return bool|int|mixed|string
     */
    public static function updateAbnormalCount($wikiId, $isAbnormal = true)
    {
        if (empty($wikiId)) {
            return false;
        }
        $info = self::getByWhere(['wiki_id' => $wikiId]);
        if (!empty($info)) {
            $abnormalCount = $isAbnormal ? ($info['abnormal_count'] + 1) : 0;
            return self::updateData(['abnormal_count' => $abnormalCount], ['wiki_id' => $wikiId]);
        } else if ($isAbnormal) {
            return self::insertData(['wiki_id' => $wikiId, 'abnormal_count' => 1]);
        }
    }

    public static function getByWhere($where, $select = '*')
    {
        return self::get($select, $where);
    }

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);
        return $list;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);
        return $count;
    }

    public static function insertData($data)
    {
        if (!isset($data['addtime']) || !$data['addtime']) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

}